<?php
/*
 * 小分类
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
use shu\Tree;
class Categoryz extends Comm
{
    public function index()
    {
        $category = db('category')->field('id,pid,name,o')->order('o asc')->select();
        $category = $this->getMenu($category);
        $this->assign('category', $category);
        return $this->fetch();
    }
    public function edit()
    {
        $id = input('param.id');
        $currentcategory = db('category')->where('id',$id)->find();
        $this->assign('currentcategory', $currentcategory);
        $category = db('category')->field('id,pid,name')->where('id','neq',$id)->order('o asc')->select();
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str, $currentcategory['pid']);
        $this->assign('category', $category);
		return $this->fetch('form');
    }
	
    public function add($pid=null)
    {
		$pid = input('param.pid');
        $category = db('category')->field('id,pid,name')->order('o asc')->select();
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str, $pid);
        $this->assign('category', $category);
        return $this->fetch('form');
    }
	
    public function update($act = null,$classid=0)
    {
        if ($act == 'order') {
            $id = input('post.id', 0, 'intval');
            if (!$id) {
                return json(array('code' => 0, 'msg' => '失败'));
            }
            $o = input('post.o', 0, 'intval');
            db('category')->data(array('o' => $o))->where('id',$id)->update();
            addlog('分类修改排序，ID：' . $id);
			return json(array('code' => 200, 'msg' => '排序成功'));
        }
		
        $id = input('post.id', false, 'intval');
        $data['type'] = input('post.type', 0, 'intval');
        $data['pid'] = input('post.pid', 0, 'intval');
        $data['name'] = input('post.name','');
		$data['dir'] = input('post.dir','',array('strip_tags','trim'));
        $data['seotitle'] = input('post.seotitle', '', 'htmlspecialchars');
        $data['keywords'] = input('post.keywords', '', 'htmlspecialchars');
        $data['description'] = input('post.description', '', 'htmlspecialchars');
        $data['content'] = input('post.content','');
        $data['url'] = input('post.url','');
        $data['cattemplate'] = input('post.cattemplate','');
        $data['contemplate'] = input('post.contemplate','');
        $data['o'] = input('post.o', 0, 'intval');
		if ($data['name'] == '') {
            $this->error('分类名称不能为空！');
        }
		
		
		if($data['pid']==0){
			$data['path'] = 0;
			$data['deep']=1;
		}else{
			$data1 = db('category')->where(array('id'=>$data['pid']))->find();
			$data['path'] = $data1['path'].','.$data['pid'];
			$data['deep']=$data1['deep']+1;	
		}
		
		
        if ($id) {
            if (db('category')->data($data)->where('id',$id)->update()) {
                addlog('文章分类修改，ID：' . $id . '，名称：' . $data['name']);
                //$this->success('恭喜，分类修改成功！');
				return json(array('code' => 200, 'msg' => '恭喜，分类修改成功！'));
                die(0);
            }
        } else {
            $id = db('category')->data($data)->insert();
            if ($id) {
                addlog('新增分类，ID：' . $id . '，名称：' . $data['name']);
                return json(array('code' => 200, 'msg' => '恭喜，分类增加成功！'));
                die(0);
            }
        }
        //$this->success('恭喜，操作成功！');
		return json(array('code' => 200, 'msg' => '恭喜，操作成功！'));
    }
	
    public function del()
    {
        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        if ($id) {
            $data['id'] = $id;
            $category = db('category');
            if ($category->where('pid',$id)->count()) {
				return json(array('code' => 100, 'msg' => '该类下拥有子类，无法删除。'));
            } else {
                $data = db('article')->where('sid',$id)->select();
				foreach($data as $k=>$v){
					if($v['thumbnail']){
						$Imge = explode('|', $v['thumbnail']);
						foreach($Imge as $kk=>$vv){
							if(file_exists('.'.$vv)){
								unlink('.'.$vv); 
							} 
						}
					}
				}
                addlog('删除分类下文章里的图片，分类ID：' . $id);
                db('article')->where('sid',$id)->delete(); //删除分类里的文章
                addlog('删除分类下的文章，分类ID：' . $id);
                $category->where('id',$id)->delete();
                addlog('删除分类，ID：' . $id);
            }
			return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
	
	
	
	
}
?>